﻿#include "FoodOrderTable.h"

QSqlDatabase db4 = QSqlDatabase::database("KFC");   //获取指向数据库的连接

FoodOrderTable::FoodOrderTable(QObject *parent) : QObject(parent)
{

}

//数据库操作:创建订单表food_order_table
void FoodOrderTable::createFoodOrderTable(){
    QSqlQuery query(db4);
    /**
      * @leiteorz
      * bill:订单内容
      * price:总价格
      * user_name:下单人名字
      * user_phone:下单人联系电话
      */
    const QString cmd = R"(
                        create table food_order_table(id integer primary key,
                        bill varchar(200),
                        price integer,
                        user_name varchar(50),
                        u_id varchar(50),
                        user_phone varchar(50));)";
    query.exec(cmd);    //执行建表语句
}

//数据库操作:向food_order_table中插入食物订单
void FoodOrderTable::insertFoodOrder(FoodOrder food_order){
    QSqlQuery query(db4);
    //数据插入(预分配空间)
    query.prepare("insert into food_order_table values(?,?,?,?,?,?);");
    //绑定要插入的数据
    query.bindValue(1,food_order.bill);
    query.bindValue(2,food_order.price);
    query.bindValue(3,food_order.user_name);
    query.bindValue(4,food_order.u_id);
    query.bindValue(5,food_order.user_phone);
//    //调用exec()函数在query中插入上述值
//        if(query.exec()){
//            qDebug()<<"init food_table success";
//        }else{
//            //打印sql语句错误信息
//            qDebug()<<"init food_table error"<<query.lastError();
//        }
    query.exec();
}

//查询所有的订单
FoodOrder* FoodOrderTable::queryAllFoodOrder(){
    QSqlQuery query(db4);

    const QString cmd = "select * from food_order_table";
    query.exec(cmd);

    FoodOrder* foodOrder = new FoodOrder[100];
    int i = 0;

    while(query.next()){
        foodOrder[i].id = query.value(0).toInt();
        foodOrder[i].bill = query.value(1).toString();
        foodOrder[i].price = query.value(2).toInt();
        foodOrder[i].user_name = query.value(3).toString();
        foodOrder[i].u_id = query.value(4).toString();
        foodOrder[i].user_phone = query.value(5).toString();
        i++;
    }

    return foodOrder;
}

//根据id查询订单
FoodOrder FoodOrderTable::queryFoodOrderById(int id){
    QSqlQuery query(db4);

    const QString cmd = "select * from food_order_table where id = " + QString::number(id);
    query.exec(cmd);

    FoodOrder foodOrder;

    while(query.next()){
        foodOrder.id = query.value(0).toInt();
        foodOrder.bill = query.value(1).toString();
        foodOrder.price = query.value(2).toInt();
        foodOrder.user_name = query.value(3).toString();
        foodOrder.u_id = query.value(4).toString();
        foodOrder.user_phone = query.value(5).toString();
    }

    return foodOrder;
}

//根据账户查订单
FoodOrder* FoodOrderTable::queryFoodOrderByAccount(QString account){
    QSqlQuery query(db4);

    const QString cmd = "select * from food_order_table where u_id = '" + account + "'";
    query.exec(cmd);

    FoodOrder* foodOrder = new FoodOrder[100];
    int i = 0;

    while(query.next()){
        foodOrder[i].id = query.value(0).toInt();
        foodOrder[i].bill = query.value(1).toString();
        foodOrder[i].price = query.value(2).toInt();
        foodOrder[i].user_name = query.value(3).toString();
        foodOrder[i].u_id = query.value(4).toString();
        foodOrder[i].user_phone = query.value(5).toString();
        i++;
    }

    return foodOrder;
}

//根据id删除订单
void FoodOrderTable::deleteFoodOrder(int id){
    QSqlQuery query(db4);

    const QString cmd = "delete from food_order_table where id=" + QString::number(id);

    query.exec(cmd);
}
